package com.huanggf.meituan.fooddetail;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.huanggf.meituan.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class FoodDetailActivity extends AppCompatActivity {

    private TextView backforward;
    private SearchView searchShop;
    private ListView shop_datas;
    private SQLiteDatabase db;

    private List<FoodDetails> getQueryShopList(Cursor c) {
        List<FoodDetails> foodDetailsList = new ArrayList<>();
        while (c.moveToNext()) {
            FoodDetails foodDetails = new FoodDetails();
            foodDetails.setId(c.getInt(0));
            foodDetails.setShopLogo(c.getString(1));
            foodDetails.setShopName(c.getString(2));
            foodDetails.setGrade(c.getDouble(3));
            foodDetails.setSellNumber(c.getInt(4));
            foodDetails.setTime(c.getString(5));
            foodDetails.setDistance(c.getString(6));
            foodDetails.setStartPrice(c.getString(7));
            foodDetails.setPrice(c.getString(8));
            foodDetails.setAvgPrice(c.getString(9));
            foodDetails.setAd1(c.getString(10));
            foodDetails.setAd2(c.getString(11));
            foodDetails.setDiscount1(c.getString(12));
            foodDetails.setDiscount2(c.getString(13));
            foodDetails.setDiscount3(c.getString(14));
            foodDetails.setDiscount4(c.getString(15));
            foodDetails.setSendTypePath(c.getString(16));
            foodDetailsList.add(foodDetails);
        }
        c.close();
        return foodDetailsList;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food_detail);
        backforward = (TextView) findViewById(R.id.backforward);
        searchShop = (SearchView) findViewById(R.id.searchShop);
        shop_datas = (ListView) findViewById(R.id.shop_datas);
        backforward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                setResult(1, intent);
                finish();
            }
        });

        //构造数据
        File file = new File("/data/data/com.huanggf.meituan/food_detail");
        if (!file.exists())
            file.mkdirs();
        //创建数据库
        db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.huanggf.meituan/databases/meituan.db", null);

        //造数据开始
        try {
            //输入流 获得图片文件路径
            InputStream inputStream = getResources().openRawResource(R.raw.dgj);
            byte[] datas = new byte[inputStream.available()];
            inputStream.read(datas);
            inputStream.close();
            FileOutputStream outShopLogo = new FileOutputStream("/data/data/com.huanggf.meituan/food_detail/dgj.png");
            outShopLogo.write(datas);
            outShopLogo.close();
        } catch (IOException e) {
            Log.e("IOException", e.getMessage());
        }

        try {
            InputStream inputStream = getResources().openRawResource(R.raw.zsb);
            byte[] datas = new byte[inputStream.available()];
            inputStream.read(datas);
            inputStream.close();
            FileOutputStream outShopLogo = new FileOutputStream("/data/data/com.huanggf.meituan/food_detail/zsb.png");
            outShopLogo.write(datas);
            outShopLogo.close();
        } catch (IOException e) {
            Log.e("IOException", e.getMessage());
        }

        try {
            //天坑注意
            InputStream inputStream = getResources().openRawResource(R.raw.hwfs);
            byte[] datas = new byte[inputStream.available()];
            inputStream.read(datas);
            inputStream.close();
            FileOutputStream outShopLogo = new FileOutputStream("/data/data/com.huanggf.meituan/food_detail/hwfs.png");
            outShopLogo.write(datas);
            outShopLogo.close();
        } catch (IOException e) {
            Log.e("IOException", e.getMessage());
        }

        try {
            InputStream inputStream = getResources().openRawResource(R.raw.spec_food);
            byte[] datas = new byte[inputStream.available()];
            inputStream.read(datas);
            inputStream.close();
            FileOutputStream outShopLogo = new FileOutputStream("/data/data/com.huanggf.meituan/food_detail/spec_food.png");
            outShopLogo.write(datas);
            outShopLogo.close();
        } catch (IOException e) {
            Log.e("IOException", e.getMessage());
        }

        //做测试用的数据
        //测试数据一
        ContentValues test1 = new ContentValues();
        test1.put("shop_logo", "/data/data/com.huanggf.meituan/food_detail/jlzj.png");
        test1.put("shop_name", "叫了只鸡1");
        test1.put("grade", 4.8);
        test1.put("sell_number", 3472);
        test1.put("time", "10分钟");
        test1.put("distance", "11km");
        test1.put("start_price", "20");
        test1.put("price", "30");
        test1.put("avg_price", "15");
        test1.put("ad1", "\"炸鸡炸串人气第一\"");
        test1.put("ad2", "美味实惠");
        test1.put("discount1", "已领10元券");
        test1.put("discount2", "32减3");
        test1.put("discount3", "30减5");
        test1.put("discount4", "50减10");
        test1.put("send_type_path", "/data/data/com.huanggf.meituan/food_detail/spec_food.png");
        db.insert("food_detail", null, test1);

        //测试数据二
        ContentValues test2 = new ContentValues();
        test2.put("shop_logo", "/data/data/com.huanggf.meituan/food_detail/yxfs.png");
        test2.put("shop_name", "鸭血粉丝汤2");
        test2.put("grade", 5.0);
        test2.put("sell_number", 2392);
        test2.put("time", "22分钟");
        test2.put("distance", "20km");
        test2.put("start_price", "20");
        test2.put("price", "30");
        test2.put("avg_price", "15");
        test2.put("ad1", "\"口感鲜美\"");
        test2.put("ad2", "价格实惠");
        test2.put("discount1", "已领6元券");
        test2.put("discount2", "32减3");
        test2.put("discount3", "30减5");
        test2.put("discount4", "50减10");
        test2.put("send_type_path", "/data/data/com.huanggf.meituan/food_detail/zsb.png");
        db.insert("food_detail", null, test2);

        //测试数据三
        ContentValues test3 = new ContentValues();
        test3.put("shop_logo", "/data/data/com.huanggf.meituan/food_detail/lsf.png");
        test3.put("shop_name", "柳州螺蛳粉3");
        test3.put("grade", 4.9);
        test3.put("sell_number", 3389);
        test3.put("time", "10分钟");
        test3.put("distance", "11km");
        test3.put("start_price", "20");
        test3.put("price", "30");
        test3.put("avg_price", "15");
        test3.put("ad1", "\"人气超高\"");
        test3.put("ad2", "广西人最爱");
        test3.put("discount1", "已领2元券");
        test3.put("discount2", "32减3");
        test3.put("discount3", "30减5");
        test3.put("discount4", "50减10");
//        test3.put("send_type_path", "/data/data/com.huanggf.meituan/food_detail/spec_food.png");
        db.insert("food_detail", null, test3);

        //测试数据四
        ContentValues test4 = new ContentValues();
        test4.put("shop_logo", "/data/data/com.huanggf.meituan/food_detail/kaorou.png");
        test4.put("shop_name", "秘制烤肉4");
        test4.put("grade", 4.4);
        test4.put("sell_number", 2761);
        test4.put("time", "10分钟");
        test4.put("distance", "11km");
        test4.put("start_price", "20");
        test4.put("price", "30");
        test4.put("avg_price", "15");
        test4.put("ad1", "\"味道很正\"");
        test4.put("ad2", "回头客居多");
        test4.put("discount1", "已领5元券");
        test4.put("discount2", "32减3");
        test4.put("discount3", "30减5");
        test4.put("discount4", "50减10");
        test4.put("send_type_path", "/data/data/com.huanggf.meituan/food_detail/spec_food.png");
        db.insert("food_detail", null, test4);

        //测试数据五
        ContentValues test5 = new ContentValues();
        test5.put("shop_logo", "/data/data/com.huanggf.meituan/food_detail/pbf.png");
        test5.put("shop_name", "小宝煲仔饭5");
        test5.put("grade", 4.9);
        test5.put("sell_number", 1111);
        test5.put("time", "10分钟");
        test5.put("distance", "11km");
        test5.put("start_price", "20");
        test5.put("price", "30");
        test5.put("avg_price", "15");
        test5.put("ad1", "\"美味\"");
        test5.put("ad2", "实惠");
        test5.put("discount1", "已领3元券");
        test5.put("discount2", "32减3");
        test5.put("discount3", "30减5");
        test5.put("discount4", "50减10");
        test5.put("send_type_path", "/data/data/com.huanggf.meituan/food_detail/spec_food.png");
        db.insert("food_detail", null, test5);
        //造数据结束

        //查询数据集,判断是否是其他页面携带查询条件进来的
        Bundle datas = this.getIntent().getExtras();
        List<FoodDetails> foodDetailsList = null;
        if (datas != null && !datas.isEmpty()) {
            String querySQL = datas.getString("querySQL");
            String queryData = datas.getString("queryData");
            searchShop.setQuery(queryData,false);
            Cursor c = db.rawQuery(querySQL,null);
            foodDetailsList = getQueryShopList(c);
        } else {
            String selFoodShop = "SELECT id,shop_logo,shop_name,grade,sell_number,time,distance,start_price,price" +
                    ",avg_price,ad1,ad2,discount1,discount2,discount3,discount4,send_type_path FROM food_detail";
            Cursor c = db.rawQuery(selFoodShop, null);
            foodDetailsList = getQueryShopList(c);
        }
        FoodDetailAdapter foodDetailAdapter = new FoodDetailAdapter(this, foodDetailsList);
        shop_datas.setAdapter(foodDetailAdapter);

        //搜索功能
        searchShop.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String selFoodShop = "SELECT id,shop_logo,shop_name,grade,sell_number,time,distance,start_price,price" +
                        ",avg_price,ad1,ad2,discount1,discount2,discount3,discount4,send_type_path FROM food_detail WHERE shop_name like ?";
                Cursor c = db.rawQuery(selFoodShop, new String[]{"%" + query + "%"});
                List<FoodDetails> foodDetailsList = getQueryShopList(c);
                FoodDetailAdapter foodDetailAdapter = new FoodDetailAdapter(FoodDetailActivity.this, foodDetailsList);
                shop_datas.setAdapter(foodDetailAdapter);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                String selFoodShop = "SELECT id,shop_logo,shop_name,grade,sell_number,time,distance,start_price,price" +
                        ",avg_price,ad1,ad2,discount1,discount2,discount3,discount4,send_type_path FROM food_detail WHERE shop_name like ?";
                Cursor c = db.rawQuery(selFoodShop, new String[]{"%" + newText + "%"});
                List<FoodDetails> foodDetailsList = getQueryShopList(c);
                FoodDetailAdapter foodDetailAdapter = new FoodDetailAdapter(FoodDetailActivity.this, foodDetailsList);
                shop_datas.setAdapter(foodDetailAdapter);
                return true;
            }
        });
    }

    @Override
    protected void onDestroy() {
        //关闭数据库
        super.onDestroy();
        if (null != db && db.isOpen())
            db.close();
    }
}
